home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Freeware / Griffith 0.9.8 / griffith-0.9.8-win32.exe / {app} / lib / gconsole.py < prev    next >
Text File  |  2008-11-17  |  6KB  |  208 lines

  1. # -*- coding: ISO-8859-1 -*-
  2.  
  3. __revision__ = '$Id: gconsole.py 1040 2008-11-15 21:13:49Z mikej06 $'
  4.  
  5. # Copyright (c) 2005-2008 Vasco Nunes, Piotr O┼╝arowski
  6.  
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU Library General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  20.  
  21. # You may use and distribute this software under the terms of the
  22. # GNU General Public License, version 2 or later
  23.  
  24. import getopt
  25. import sys
  26. import gutils
  27.  
  28. options = ('hDCo:t:d:c:y:s:', ('help', 'debug', 'sqlecho', 'clean', 'check-dep',
  29.     'show-dep', 'original_title=', 'title=', 'director=', 'cast=', 'year=',
  30.     'sort=', 'seen=', 'loaned=', 'number=', 'runtime=', 'rating=', 'home=',
  31.     'config='))
  32.  
  33. def check_args(self):
  34.     self._tmp_home = None
  35.     self._tmp_config = 'griffith.cfg'
  36.     if len(sys.argv)>1:
  37.         try:
  38.             opts, args = getopt.getopt(sys.argv[1:], options[0], options[1])
  39.         except getopt.GetoptError:
  40.             # print help information and exit:
  41.             con_usage()
  42.             sys.exit(2)
  43.         
  44.         for o, a in opts:
  45.             if o in ('-h', '--help'):
  46.                 con_usage()
  47.                 sys.exit()
  48.             elif o in ('-D', '--debug'):
  49.                 self.debug.set_debug()
  50.             elif o == '--home':
  51.                 self._tmp_home = a # see initialize.locations()
  52.             elif o == '--config':
  53.                 self._tmp_config = a # griffith.__init__
  54.             elif o == '--check-dep':
  55.                 check_dependencies()
  56.                 sys.exit()
  57.             elif o == '--show-dep':
  58.                 show_dependencies()
  59.                 sys.exit()
  60.  
  61. def check_args_with_db(self):
  62.     if len(sys.argv)>1:
  63.         try:
  64.             opts, args = getopt.getopt(sys.argv[1:], options[0], options[1])
  65.         except getopt.GetoptError:
  66.             # print help information and exit:
  67.             con_usage()
  68.             sys.exit(3)
  69.  
  70.         sort = None
  71.         where = {}
  72.         for o, a in opts:
  73.             if o in ('-C', '--clean'):
  74.                 gutils.clean_posters_dir(self)
  75.                 sys.exit()
  76.             elif o == '--sqlecho':
  77.                 self.db.metadata.engine.echo = True
  78.             elif o in ('-s', '--sort'):
  79.                 sort = a
  80.             elif o in ('-o', '--original_title'):
  81.                 where['o_title'] = a
  82.             elif o in ('-t', '--title'):
  83.                 where['title'] = a
  84.             elif o in ('-d', '--director'):
  85.                 where['director'] = a
  86.             elif o in ('-c', '--cast'):
  87.                 where['cast'] = a
  88.             elif o in ('-y', '--year'):
  89.                 where['year'] = str(int(a))
  90.             elif o == '--seen':
  91.                 where['seen'] = a
  92.             elif o == '--loaned':
  93.                 where['loaned'] = a
  94.             elif o == '--number':
  95.                 where['number'] = a
  96.             elif o == '--runtime':
  97.                 where['runtime'] = a
  98.             elif o == '--rating':
  99.                 where['rating'] = a
  100.         if where:
  101.             con_search_movie(self, where, sort)
  102.  
  103. def con_search_movie(self, where, sort=None):
  104.     # for search function
  105.     from sqlalchemy import select
  106.     col = lambda x: self.db.Movie.c[x]
  107.     columns = (col('number'), col('title'), col('o_title'), col('director'), col('year'))
  108.     
  109.     sort_columns = []
  110.     if sort:
  111.         for i in sort.split(','):
  112.             if self.db.Movie.c.has_key(i):
  113.                 sort_columns.append(col(i))
  114.     else:
  115.         sort_columns = [col('number')]
  116.  
  117.     statement = select(columns=columns, order_by=sort_columns)
  118.  
  119.     for i in where:
  120.         if i in ('seen', 'loaned'):    # force boolean
  121.             if where[i].lower() in ('0', 'no', 'n'):
  122.                 where[i] = False
  123.             else:
  124.                 where[i] = True
  125.         if i in ('year', 'number', 'runtime', 'seen', 'loaned', 'rating'):
  126.             statement.append_whereclause(col(i)==where[i])
  127.         else:
  128.             statement.append_whereclause(col(i).like('%' + where[i] + '%' ))
  129.  
  130.     movies = statement.execute().fetchall()
  131.     if not movies:
  132.         print _("No movie found")
  133.     else:
  134.         for movie in movies:
  135.             print "\033[31;1m[%s]\033[0m\t\033[38m%s\033[0m (\033[35m%s\033[0m), %s - \033[32m%s\033[0m" % \
  136.                 (movie.number, movie.title, movie.o_title, movie.year, movie.director)
  137.     sys.exit()
  138.  
  139. def check_dependencies():
  140.     ostype = None
  141.     if sys.version.rfind('Debian'):
  142.         ostype = 'debian'
  143.  
  144.     (missing, extra) = gutils.get_dependencies()
  145.  
  146.     def __print_missing(modules):
  147.         import string
  148.         missing = ''
  149.         for i in modules:
  150.             if i['version']==False or (not isinstance(i['version'], bool) and i['version'].startswith('-')):
  151.                 tmp = None
  152.                 if ostype is not None:
  153.                     if ostype == 'debian' and i.has_key('debian'):
  154.                         tmp = "\n%s package" % i['debian']
  155.                         if i.has_key('debian_req') and i['debian_req'] is not None:
  156.                             tmp += "\n\tminimum required package version: %s" % i['debian_req']
  157.                 if tmp is None:
  158.                     tmp = "\n%s module" % i['module']
  159.                     if i.has_key('module_req') and i['module_req'] is not None:
  160.                         tmp += "\n\tminimum required module version: %s" % i['module_req']
  161.                     if i.has_key('url'):
  162.                         tmp += "\n\tURL: %s" % i['url']
  163.                 if i['version'] is not False and i['version'].startswith('-'):
  164.                     tmp += "\n\tavailable module version: %s" % i['version'][1:]
  165.                 if tmp is not None:
  166.                     missing += tmp
  167.         if missing == '':
  168.             return None
  169.         else:
  170.             return missing
  171.  
  172.     tmp = __print_missing(missing)
  173.     if tmp is not None:
  174.         print 'Dependencies missing:'
  175.         print '===================='
  176.         print tmp
  177.     tmp = __print_missing(extra)
  178.     if tmp is not None:
  179.         print '\n\nOptional dependencies missing:'
  180.         print '============================='
  181.         print tmp, "\n"
  182.  
  183. def show_dependencies():
  184.     (missing, extra) = gutils.get_dependencies()
  185.     for i in missing:
  186.         print "%(module)s :: %(version)s" % i
  187.     for i in extra:
  188.         print "%(module)s :: %(version)s" % i
  189.  
  190. def con_usage():
  191.     print "USAGE:", sys.argv[0], '[OPTIONS]'
  192.     print "\nOPTIONS:"
  193.     print "-h, --help\tprints this screen"
  194.     print "-D, --debug\trun with more debug info"
  195.     print "-C, --clean\tfind and delete orphan files in posters directory"
  196.     print "--check-dep\tcheck dependencies"
  197.     print "--show-dep\tshow dependencies"
  198.     print "--sqlecho\tprint SQL queries"
  199.     print "--home DIR \tset Griffith's home directory (instead of the default ~/.griffith)"
  200.     print "\n printing movie list:"
  201.     print "-c <expr>, --cast=<expr>"
  202.     print "-d <expr>, --director=<expr>"
  203.     print "-o <expr>, --original_title=<expr>"
  204.     print "-t <expr>, --title=<expr>"
  205.     print "-y <number>, --year=<number>"
  206.     print "-s <columns>, --sort=<columns>"
  207.  
  208.